//
//  ICKOrderTableViewCell.m
//  iComeKernel
//
//  Created by zhangrongwu on 2018/5/18.
//  Copyright © 2018年 ENN-Zhang Xin. All rights reserved.
//

#import "ICKOrderTableViewCell.h"
#import "Masonry.h"
#import <SDWebImage/UIImageView+WebCache.h>
#define NRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:1.0]

// 订单
@interface ICOrderIdView : UIView
@property (nonatomic, strong)UIImageView *imageV;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *statusLabel;
@property (nonatomic, strong)UIView *line;
@end

@implementation ICOrderIdView
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.imageV];
        [self addSubview:self.titleLabel];
        [self addSubview:self.statusLabel];
        [self addSubview:self.line];
        
        [self.imageV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.centerY.equalTo(self.mas_centerY);
        }];
        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.imageV.mas_right).offset(15);
            make.centerY.equalTo(self.mas_centerY);
        }];
        
        [self.statusLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-15);
            make.centerY.equalTo(self.mas_centerY);
        }];
        
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo(0.5);
        }];
        
        self.titleLabel.text = @"订单号：10012038384";
        self.statusLabel.text = @"进行中";
    }
    return self;
}

-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        _imageV.image = [UIImage imageNamed:@"im_item_order_status"];
    }
    return _imageV;
}
-(UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = NRGB(0x585858);
    }
    return _titleLabel;
}

-(UILabel *)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [[UILabel alloc] init];
        _statusLabel.font = [UIFont systemFontOfSize:14];
        _statusLabel.textColor = NRGB(0x3498FE);
    }
    return _statusLabel;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = NRGB(0xE0E0E0);
    }
    return _line;
}

@end

//  需求方
@interface ICOrderFromView : UIView
@property (nonatomic, strong)UIImageView *imageV;
@property (nonatomic, strong)UILabel *messageLabel;
@property (nonatomic, strong)UILabel *contactLabel;
@property (nonatomic, strong)UIView *line;

@end

@implementation ICOrderFromView
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.imageV];
        [self addSubview:self.messageLabel];
        [self addSubview:self.contactLabel];
        [self addSubview:self.line];
        
        [self.imageV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.centerY.equalTo(self.mas_centerY);
        }];
        
        [self.messageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.imageV.mas_right).offset(15);
            make.top.equalTo(self.mas_top).offset(10);
        }];
        
        [self.contactLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.imageV.mas_right).offset(15);
            make.top.equalTo(self.messageLabel.mas_bottom).offset(8);
        }];
        
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo(0.5);
        }];
        
        self.messageLabel.text = @"需求方：洗脑集团资源网络";
        self.contactLabel.text = @"张荣武 17600105766";
    }
    return self;
}

-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        _imageV.image = [UIImage imageNamed:@"im_item_order_reqs"];
    }
    return _imageV;
}

-(UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.font = [UIFont systemFontOfSize:14];
        _messageLabel.textColor = NRGB(0x585858);
    }
    return _messageLabel;
}

-(UILabel *)contactLabel {
    if (!_contactLabel) {
        _contactLabel = [[UILabel alloc] init];
        _contactLabel.font = [UIFont systemFontOfSize:14];
        _contactLabel.textColor = NRGB(0x585858);
    }
    return _contactLabel;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = NRGB(0xE0E0E0);
    }
    return _line;
}
@end

// 订单
@interface ICOrderInfoView : UIView
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIImageView *imageV;
@property (nonatomic, strong)UILabel *descTitleLabel;
@property (nonatomic, strong)UILabel *priceLabel;
@property (nonatomic, strong)UILabel *noteLabel;
@property (nonatomic, strong)UILabel *totalLabel;
@property (nonatomic, strong)UIView *keyValueView;
@property (nonatomic, strong)UIView *line;
@property (nonatomic, strong)UIView *line_1;
@property (nonatomic, strong)UIView *line_2;
@property (nonatomic, strong)UILabel *endLabel; // 隐藏
@end

@implementation ICOrderInfoView
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.line_1];
        [self addSubview:self.imageV];
        [self addSubview:self.descTitleLabel];
        [self addSubview:self.priceLabel];
        [self addSubview:self.noteLabel];
        [self addSubview:self.totalLabel];
        [self addSubview:self.line_2];
        [self addSubview:self.keyValueView];
        [self addSubview:self.line];
        [self addSubview:self.endLabel];
        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.top.equalTo(self.mas_top).offset(10);
        }];
        
        [self.line_1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(40);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(0.5);
        }];
        
        [self.imageV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(55);
            make.left.equalTo(self.mas_left).offset(15);
            make.height.mas_equalTo(75);
            make.width.mas_equalTo(75);
        }];
        
        [self.descTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageV.mas_top);
            make.left.equalTo(self.imageV.mas_right).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
        }];
        
        [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.descTitleLabel.mas_bottom);
            make.left.equalTo(self.descTitleLabel.mas_left);
        }];
        
        [self.noteLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.priceLabel.mas_bottom);
            make.left.equalTo(self.priceLabel.mas_left);
        }];
        
        [self.totalLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageV.mas_bottom);
            make.right.equalTo(self.mas_right).offset(-15);
        }];
        
        [self.line_2 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.totalLabel.mas_bottom).offset(7.5);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(0.5);
        }];
        
        self.titleLabel.text = @"新奥集团人力资源共享服务中心";
        self.descTitleLabel.text = @"廊坊开发区高等学历人才引进工作对接服务";
        self.priceLabel.text = @"¥ 2,000";
        self.noteLabel.text = @"一次性交付|服务期满付款";
        self.totalLabel.text = @"合计：¥ 2,000";
        [self.imageV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"placehoder_80"]];
        // !!!放到setdata中进行处理
        NSMutableArray *list = [[NSMutableArray alloc] init];
        for (int i = 0; i < 5; i++) {
            UILabel *label = [[UILabel alloc] init];
            label.text = [NSString stringWithFormat:@"这是第%d个label，用于测试", i];
            label.font = [UIFont systemFontOfSize:14];
            label.textColor = NRGB(0x585858);
            [list addObject:label];
            [self.keyValueView addSubview:label];
        }
        
        [list mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:1 leadSpacing:1 tailSpacing:1];
        [list mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
        }];
        
        [self.keyValueView mas_remakeConstraints:^(MASConstraintMaker *make) {
            UILabel *label = (UILabel *)[list lastObject];
            make.top.equalTo(self.totalLabel.mas_bottom).offset(15);
            make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(label.mas_bottom);
        }];
        
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo(0.5);
        }];
       
        // 绘制虚线
        [self drawLineOfDashByCAShapeLayer:self.line_1 lineLength:5 lineSpacing:3 lineColor:NRGB(0xE0E0E0) lineDirection:YES];
        [self drawLineOfDashByCAShapeLayer:self.line_2 lineLength:5 lineSpacing:3 lineColor:NRGB(0xE0E0E0) lineDirection:YES];
        
        [self.endLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.keyValueView.mas_bottom).offset(10);
            make.bottom.equalTo(self.mas_bottom);
        }];
    }
    return self;
}

-(UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = NRGB(0x585858);
    }
    return _titleLabel;
}


-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
    }
    return _imageV;
}

-(UILabel *)descTitleLabel {
    if (!_descTitleLabel) {
        _descTitleLabel = [[UILabel alloc] init];
        _descTitleLabel.numberOfLines = 2;
        _descTitleLabel.font = [UIFont systemFontOfSize:14];
        _descTitleLabel.textColor = NRGB(0x222222);
    }
    return _descTitleLabel;
}

-(UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont systemFontOfSize:14];
        _priceLabel.textColor = NRGB(0x222222);
    }
    return _priceLabel;
}

-(UILabel *)noteLabel {
    if (!_noteLabel) {
        _noteLabel = [[UILabel alloc] init];
        _noteLabel.font = [UIFont systemFontOfSize:12];
        _noteLabel.textColor = NRGB(0x969696);
    }
    return _noteLabel;
}

-(UILabel *)totalLabel {
    if (!_totalLabel) {
        _totalLabel = [[UILabel alloc] init];
        _totalLabel.font = [UIFont systemFontOfSize:16];
        _totalLabel.textColor = NRGB(0x101010);
    }
    return _totalLabel;
}

-(UIView *)keyValueView {
    if (!_keyValueView) {
        _keyValueView = [[UIView alloc] init];
    }
    return _keyValueView;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = NRGB(0xE0E0E0);
    }
    return _line;
}

-(UIView *)line_1 {
    if (!_line_1) {
        _line_1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 60, 0.5)];
    }
    return _line_1;
}

-(UIView *)line_2 {
    if (!_line_2) {
        _line_2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 60, 0.5)];
    }
    return _line_2;
}

-(UILabel *)endLabel {
    if (!_endLabel) {
        _endLabel = [[UILabel alloc] init];
    }
    return _endLabel;
}

- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    if (isHorizonal) {
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {
        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

@end

// 订单相关操作
@interface ICOrderSubmitView : UIView
@property (nonatomic, strong)UIButton *cancelOrder;
@property (nonatomic, strong)UIButton *sureOrder;
@end

@implementation ICOrderSubmitView
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.cancelOrder];
        [self addSubview:self.sureOrder];
        
        [self.cancelOrder mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.right.equalTo(self.sureOrder.mas_left).offset(-15);
            make.height.mas_equalTo(25);
            make.width.mas_equalTo(70);
        }];
        
        [self.sureOrder mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(25);
            make.width.mas_equalTo(70);
        }];
    }
    return self;
}

- (void)cancelOrderAction {
    NSLog(@"取消订单");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否取消订单" message:@"" preferredStyle:UIAlertControllerStyleAlert ];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [self cancelOrderRequestWithDict:dict];
    }];
    [alertController addAction:OKAction];
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

- (void) sureOrderAction {
    NSLog(@"确定订单");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"确认最终交付完成" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    [alertController addAction:cancelAction];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        [self requireConfirmLoadRequestWithDict:dict];
    }];
    [alertController addAction:sureAction];
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
-(UIButton *)cancelOrder {
    if (!_cancelOrder) {
        _cancelOrder = [[UIButton alloc] init];
        [_cancelOrder setTitle:@"取消订单" forState:UIControlStateNormal];
        _cancelOrder.titleLabel.font = [UIFont systemFontOfSize:14];
        _cancelOrder.layer.borderWidth = 0.5;
        [_cancelOrder setTitleColor:NRGB(0x585858) forState:UIControlStateNormal];
        _cancelOrder.layer.borderColor = NRGB(0x969696).CGColor;
        [_cancelOrder addTarget:self action:@selector(cancelOrderAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelOrder;
}

-(UIButton *)sureOrder {
    if (!_sureOrder) {
        _sureOrder = [[UIButton alloc] init];
        [_sureOrder setTitle:@"确认订单" forState:UIControlStateNormal];
        _sureOrder.titleLabel.font = [UIFont systemFontOfSize:14];
        _sureOrder.layer.borderWidth = 0.5;
        [_sureOrder setTitleColor:NRGB(0x3498FE) forState:UIControlStateNormal];
        _sureOrder.layer.borderColor = NRGB(0x3498FE).CGColor;
        [_sureOrder addTarget:self action:@selector(sureOrderAction) forControlEvents:UIControlEventTouchUpInside];

    }
    return _sureOrder;
}

@end






@interface ICKOrderTableViewCell()
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)ICOrderIdView *orderId;
@property (nonatomic, strong)ICOrderFromView *orderFrom;
@property (nonatomic, strong)ICOrderInfoView *orderInfo;
@property (nonatomic, strong)ICOrderSubmitView *orderSubmit;

@end

@implementation ICKOrderTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)cell reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:cell reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.bgView];
        self.bgView.backgroundColor = [UIColor whiteColor];
        
        [self.bgView addSubview:self.orderId];
        [self.bgView addSubview:self.orderFrom];
        [self.bgView addSubview:self.orderInfo];
        [self.bgView addSubview:self.orderSubmit];

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
    
}

- (void)layoutSubview {
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.orderId mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top);
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.height.mas_equalTo(45);
    }];
    
    [self.orderFrom mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderId.mas_bottom);
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.height.mas_equalTo(65);
    }];
    
    [self.orderInfo mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderFrom.mas_bottom);
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.bottom.equalTo(self.orderInfo.endLabel.mas_bottom);
    }];
    
    [self.orderSubmit mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderInfo.mas_bottom);
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self.bgView.mas_bottom);// 前后对应自动扩容
    }];
    
    
}

-(void)setData:(id)data {
    [super setData:data];
    [self layoutSubview];

}

-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (ICOrderIdView *)orderId {
    if (!_orderId) {
        _orderId = [[ICOrderIdView alloc] init];
    }
    return _orderId;
}

-(ICOrderFromView *)orderFrom {
    if (!_orderFrom) {
        _orderFrom = [[ICOrderFromView alloc] init];
    }
    return _orderFrom;
}

-(ICOrderInfoView *)orderInfo {
    if (!_orderInfo) {
        _orderInfo = [[ICOrderInfoView alloc] init];
    }
    return _orderInfo;
}

-(ICOrderSubmitView *)orderSubmit {
    if (!_orderSubmit) {
        _orderSubmit = [[ICOrderSubmitView alloc] init];
    }
    return _orderSubmit;
}


@end
